﻿using System.Windows.Input;
using System;
using System.Windows.Controls;
using System.Windows.Media.Imaging;
using KM.Framework.Utility;

namespace KM.Framework.Business
{
    public class CommandItem : NotifyBase
    {
        public CommandItem()
        {
        }
        public CommandItem(string icon, string toolTip, ICommand command)
        {
            Icon = icon;
            ToolTip = toolTip;
            Command = command;
        }

        public CommandItem(string icon, string toolTip, string commandName)
        {
            Icon = icon;
            ToolTip = toolTip;
            CommandName = commandName;
        }

        public CommandItem(string title, string icon, string toolTip, string commandName, bool isHideMenu = false)
            : this(icon, toolTip, commandName)
        {
            Title = title;
            IsHideMenu = isHideMenu;
        }
        public CommandItem(string title, string icon, string toolTip, ICommand command, bool isHideMenu = false)
            : this(icon, toolTip, command)
        {
            Title = title;
            IsHideMenu = isHideMenu;
        }

        private string _Title;
        public string Title
        {
            get { return _Title; }
            set
            {
                if (!object.Equals(_Title, value))
                {
                    _Title = value;
                    RaisePropertyChanged("Title");
                }
            }
        }

        private string _Icon;
        public string Icon
        {
            get { return _Icon; }
            set
            {
                if (!object.Equals(_Icon, value))
                {
                    _Icon = value;
                    RaisePropertyChanged("Icon");
                }
            }
        }

        private string _ToolTip;
        public string ToolTip
        {
            get { return _ToolTip; }
            set
            {
                if (!object.Equals(_ToolTip, value))
                {
                    _ToolTip = value;
                    RaisePropertyChanged("ToolTip");
                }
            }
        }

        public ICommand Command { get; set; }

        public string CommandName { get; set; }

        private System.Collections.ObjectModel.ObservableCollection<CommandItem> _Children;
        public System.Collections.ObjectModel.ObservableCollection<CommandItem> Children
        {
            get { return _Children; }
            set
            {
                _Children = value;
                this.RaisePropertyChanged("Children");
            }
        }

        public Image IconImage
        {
            get
            {
                if (string.IsNullOrEmpty(Icon)) return null;

                return new Image()
                {
                    Source = new BitmapImage(new Uri(Icon, UriKind.Relative))
                };
            }
        }


        /// <summary>
        /// 是否在ItemsControl列表显示时隐藏
        /// </summary>
        private bool _IsHideMenu;
        public bool IsHideMenu
        {
            get { return _IsHideMenu; }
            set
            {
                _IsHideMenu = value;
                this.RaisePropertyChanged("IsHideMenu");
            }
        }


        /// <summary>
        /// 是否显示分割线
        /// </summary>
        public bool IsSeparator { get; set; }

        /// <summary>
        /// 是否无限制
        /// </summary>
        public bool IsNoLimits { get; set; }

        public object ObjParameter { get; set; }


        private bool _IsHidden;
        public bool IsHidden
        {
            get { return _IsHidden; }
            set
            {
                _IsHidden = value;
                this.RaisePropertyChanged("IsHidden");
            }
        }

        /// <summary>
        /// 创建一个分割线
        /// </summary>
        /// <returns></returns>
        public static CommandItem CreateOneSeparator()
        {
            CommandItem cmseparator = new CommandItem();
            cmseparator.IsSeparator = true;
            cmseparator.IsHideMenu = true;
            cmseparator.IsNoLimits = true;
            return cmseparator;
        }

        public const string CommandFolderPath = "/Assets/Imgs/CommandItem";

        public static CommandItem CreateItem(string title, string iconName, string toolTip, ICommand command)
        {
            return new CommandItem(title, string.Format("{0}/{1}", CommandFolderPath, iconName), toolTip, command);
        }

        public static CommandItem CreateAddItem(ICommand command, string title = "添加", string toolTip = "添加")
        {
            return CommandItem.CreateItem(title, "add.png", title, command);
        }

        public static CommandItem CreateDeleteItem(ICommand command, string title = "删除", string toolTip = "删除")
        {
            return CommandItem.CreateItem(title, "delete.png", title, command);
        }

        public static CommandItem CreateEditItem(ICommand command, string title = "编辑", string toolTip = "编辑")
        {
            return CommandItem.CreateItem(title, "edit.png", title, command);
        }

        public static CommandItem CreateSaveItem(ICommand command, string title = "保存", string toolTip = "保存")
        {
            return CommandItem.CreateItem(title, "save.png", toolTip, command);
        }

        public static CommandItem CreateSaveAndContinueItem(ICommand command, string title = "保存并继续", string toolTip = "保存并继续")
        {
            return CommandItem.CreateItem(title, "save.png", title, command);
        }

        public static CommandItem CreateCancelItem(ICommand command, string title = "取消", string toolTip = "取消")
        {
            return CommandItem.CreateItem(title, "cancel.png", title, command);
        }

        public static CommandItem CreateOpenItem(ICommand command, string title = "打开", string toolTip = "打开")
        {
            return CommandItem.CreateItem(title, "open.png", title, command);
        }

        public static CommandItem CreateImportItem(ICommand command, string title = "导入", string toolTip = "导入")
        {
            return CommandItem.CreateItem(title, "import.png", title, command);
        }

        public static CommandItem CreateCopyItem(ICommand command, string title = "复制", string toolTip = "复制")
        {
            return CommandItem.CreateItem(title, "copy.png", title, command);
        }

        public static CommandItem CreateCutItem(ICommand command, string title = "剪切", string toolTip = "剪切")
        {
            return CommandItem.CreateItem(title, "cut.png", title, command);
        }

        public static CommandItem CreateMoveItem(ICommand command, string title = "移动", string toolTip = "移动")
        {
            return CommandItem.CreateItem(title, "move.png", title, command);
        }

        public static CommandItem CreatePasteItem(ICommand command, string title = "粘贴", string toolTip = "粘贴")
        {
            return CommandItem.CreateItem(title, "paste.png", title, command);
        }

        public static CommandItem CreateRenameItem(ICommand command, string title = "重命名", string toolTip = "重命名")
        {
            return CommandItem.CreateItem(title, "rename.png", title, command);
        }

        public static CommandItem CreateRefreshItem(ICommand command, string title = "刷新", string toolTip = "刷新")
        {
            return CommandItem.CreateItem(title, "refresh.png", title, command);
        }
    }
}
